package cn.jekun.shop.app.action;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.utils.EncryptUtil;
import cn.jekun.component.core.Constants;
import cn.jekun.shop.app.service.LoginService;
import cn.jekun.shop.common.service.RegService;
/**
 * 注册会员
 * 
 * @author twj
 * 
 */
@SuppressWarnings("serial")
public class RegAction extends AppBaseAction{
	@Resource(name = RegService.BEAN_NAME)
	RegService regService;
	@Resource(name = LoginService.BEAN_NAME)
	LoginService loginService;
	public String register(){
		JSONObject jsonObject = null;
		try {
			String mobile = this.request.getParameter("mobile");//注册手机号
			String password = this.request.getParameter("password");//注册密码
			String validateCode = this.request.getParameter("validateCode");//手机短信验证码
			String come = this.request.getParameter("come");//会员来源
			this.validateSign(mobile,password,validateCode,come);//验证签名是否正确 mobile+password+validateCode+come
			jsonObject = regService.register(mobile,password,validateCode,come,null,Constants.MC_MEMBER_TYPE.APP);
			//注册成功自动登录
			if(jsonObject.get("code").toString().equals("1")){
				//String sign = EncryptUtil.md5(mobile+password);
//				password = EncryptUtil.md5(password);
				jsonObject = loginService.loginApp(mobile, password, getSign(), null);
				}
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "注册成功，来源:mobile");//记录操作日志
			} catch (BusinessException e) {
				jsonObject = JSONObject.fromObject(e.getErrorMsg());
				this.logService.addMallLog(this.getUserId(), 
						this.getBrowser(), this.getIp(), "注册失败，原因:"+jsonObject.get("message")+",来源:mobile");//记录操作日志

			} catch (Exception e) {
				jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
				e.printStackTrace();
			}
			return this.writeJson(jsonObject);
		
	}
	/**
	 * 验证手机号是否注册
	 * @return
	 */
	public String validateMobile(){
		JSONObject jsonObject = null;
		try {
			String mobile = this.request.getParameter("mobile");//注册手机号
			this.validateSign(mobile);
			jsonObject = regService.validateMobile(mobile);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	
	}
	/**
	 * 发送验证码
	 * @return
	 */
	public String sendValidateCode(){
		JSONObject jsonObject = null;
		try {
			String mobile = this.request.getParameter("mobile");//注册手机号
			this.validateSign(mobile);
			jsonObject = regService.sendValidateCodeByReg(mobile);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	public String registerByOthers(){
		JSONObject jsonObject = null;
		try {
			String come = "";
			String mobile = this.request.getParameter("mobile");//注册手机号
			String password = this.request.getParameter("password");//注册密码
			String validateCode = this.request.getParameter("validateCode");//手机短信验证码
			String shopId = this.request.getParameter("shopId");//会员来源
			String userId = this.request.getParameter("tempUserId");//会员发展会员
			String terminalId = this.request.getParameter("terminalId");//终端代码
			if(StringUtils.isBlank(shopId)&&StringUtils.isNotBlank(userId)){
				come = userId;
			}
			if(StringUtils.isNotBlank(shopId)&&StringUtils.isBlank(userId)){
				come = shopId;
			}
//			this.validateSign(mobile,password,validateCode,come);//验证签名是否正确 mobile+password+validateCode+come
			jsonObject = regService.register(mobile,password,validateCode,come,terminalId,Constants.MC_MEMBER_TYPE.APP);
			//注册成功自动登录
			} catch (BusinessException e) {
				jsonObject = JSONObject.fromObject(e.getErrorMsg());
			} catch (Exception e) {
				jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
				e.printStackTrace();
			}
		return this.writeJson(jsonObject);
	}

}
